package com.beasts.ant.auth.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.util.UriComponentsBuilder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * TODO 转接 /oauth/*
 * @author Locker
 * @date 16/05/2018 3:42 PM
 * @since 1.0
 */
@Controller
@RequestMapping("/auth")
public class AuthController {

    @RequestMapping("/oauth/token")
    public void oauthToken(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String authorizeUrl = "http://localhost:9999/oauth/token";
        Map<String, String> reqParams = new HashMap<String, String>();
        reqParams.put("client_id", "client_ant_page");
        reqParams.put("client_secret", "client_ant_page_secret");
        reqParams.put("scope", "select");
//        reqParams.put("redirect_uri", "http://localhost:9999/authentication/userInfo");
        reqParams.put("redirect_uri", "http://www.sina.com");
        reqParams.put("grant_type", "password");
        reqParams.put("username", "u1");
        reqParams.put("password", "p1");

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(authorizeUrl);
        for (Map.Entry<String, String> param : reqParams.entrySet()) {
            builder.queryParam(param.getKey(), param.getValue());
        }

        String redirectUrl = resp.encodeRedirectURL(builder.build().encode().toUriString());

        resp.sendRedirect(redirectUrl);
    }

}
